package ${packageName};

<#list imports as import>
import ${import};
</#list>
import ${basePackage}.param.${underlineToCamel(sourceTable?lower_case,true?string)}Param;
import ${basePackage}.mapper.${underlineToCamel(sourceTable?lower_case,true?string)}Mapper;
import ${basePackage}.entity.${underlineToCamel(sourceTable?lower_case,true?string)}Entity;
import ${basePackage}.service.${underlineToCamel(sourceTable?lower_case,true?string)}Service;
import ${basePackage}.vo.${underlineToCamel(sourceTable?lower_case,true?string)}Vo;
import ${basePackage}.converter.${underlineToCamel(sourceTable?lower_case,true?string)}VoEntityConverter;



/**
* @version V1.0
* ${sourceTableDesc}:${sourceTable} 对应商业服务实现.
* @ClassName: ${underlineToCamel(sourceTable?lower_case,true?string)}ServiceImpl
* @Description: ${sourceTableDesc}:${sourceTable} 对应商业服务实现.
* @author: ${author}
* @date:   ${.now}
* @Copyright: Copyright (c) ${.now?string('yyyy')}
*/
@Service
public class ${underlineToCamel(sourceTable?lower_case,true?string)}ServiceImpl extends ServiceImpl<${underlineToCamel(sourceTable?lower_case,true?string)}Mapper, ${underlineToCamel(sourceTable?lower_case,true?string)}Entity> implements ${underlineToCamel(sourceTable?lower_case,true?string)}Service {

    @Autowired
    private ${underlineToCamel(sourceTable?lower_case,true?string)}VoEntityConverter ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter;


    /**
    * 根据ID查询单个${sourceTableDesc}对象
    * @param id 组键ID
    * @return 单个${sourceTableDesc}VO对象
    */
    public ${underlineToCamel(sourceTable?lower_case,true?string)}Vo queryById(Serializable id){
        if (id == null) {return null;}
        return ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.reverseFromTarget(this.getById(id));
    }


    /**
    * 根据ID集合查询返回${sourceTableDesc}列表
    * @param idList 组键ID集合
    * @return ${sourceTableDesc}列表
    */
    public List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> queryByIds(Collection<? extends Serializable> idList){
        if(CollectionUtils.isEmpty(idList)){return null;}
        return ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.reverseFromTarget(this.listByIds(idList));
    }


    /**
    * 分页查询 ${sourceTableDesc} 信息
    * @param page 分页对象
    * @param param 查询参数
    * @return IPage<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> 分页对象
    */
    @Override
    public IPage<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> queryForPage(IPage page,${underlineToCamel(sourceTable?lower_case,true?string)}Param param){
      IPage<${underlineToCamel(sourceTable?lower_case,true?string)}Entity> page0 = this.page(page,createQueryWrapper(param));
      IPage<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> pageResult = page0.convert(e -> ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.reverseFromTarget(e));
      return pageResult;
    }


    /**
    * 根据条件查询返回 ${sourceTableDesc} 对象列表
    * @param  param 查询参数
    * @return ${sourceTableDesc} 对象列表
    */
    @Override
    public List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> queryForList(${underlineToCamel(sourceTable?lower_case,true?string)}Param param) {
        List<${underlineToCamel(sourceTable?lower_case,true?string)}Entity> list = this.list(createQueryWrapper(param));
        return ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.reverseFromTarget(list);
    }

    /**
    * 构建查询条件
    * @param param 查询参数实体对象
    * @return 构建后的QueryWrapper查询条件
    */
    private QueryWrapper<${underlineToCamel(sourceTable?lower_case,true?string)}Entity> createQueryWrapper(${underlineToCamel(sourceTable?lower_case,true?string)}Param param){
        QueryWrapper<${underlineToCamel(sourceTable?lower_case,true?string)}Entity> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(param)) { return queryWrapper; }
        Optional<${underlineToCamel(sourceTable?lower_case,true?string)}Param> op = Optional.of(param);
        queryWrapper.lambda()
            <#list columns as col>
                <#if col.columnName != 'tenant_id' && col.columnName != 'version' && col.columnName != 'creator' && col.columnName != 'create_time' && col.columnName != 'modifier' && col.columnName != 'modify_time'>
                    <#if getDataType(col.dataType) == 'String'>
                .eq(op.map(${underlineToCamel(sourceTable?lower_case,true?string)}Param::get${(col.columnName ? index_of('_') == 1) ? string (underlineToCamel(col.columnName?lower_case,false?string),underlineToCamel(col.columnName?lower_case,true?string))}).filter(StringUtils::isNotBlank).isPresent(),${underlineToCamel(sourceTable?lower_case,true?string)}Entity::get${(col.columnName ? index_of('_') == 1) ? string (underlineToCamel(col.columnName?lower_case,false?string),underlineToCamel(col.columnName?lower_case,true?string))},param.get${(col.columnName ? index_of('_') == 1) ? string (underlineToCamel(col.columnName?lower_case,false?string),underlineToCamel(col.columnName?lower_case,true?string))}())
                    <#else >
                .eq(op.map(${underlineToCamel(sourceTable?lower_case,true?string)}Param::get${(col.columnName ? index_of('_') == 1) ? string (underlineToCamel(col.columnName?lower_case,false?string),underlineToCamel(col.columnName?lower_case,true?string))}).isPresent(),${underlineToCamel(sourceTable?lower_case,true?string)}Entity::get${(col.columnName ? index_of('_') == 1) ? string (underlineToCamel(col.columnName?lower_case,false?string),underlineToCamel(col.columnName?lower_case,true?string))},param.get${(col.columnName ? index_of('_') == 1) ? string (underlineToCamel(col.columnName?lower_case,false?string),underlineToCamel(col.columnName?lower_case,true?string))}())
                    </#if>
                </#if>
            </#list>
        ;
        return queryWrapper;
    }

    /**
    * 添加${sourceTableDesc}到数据库
    * @param vo  ${sourceTableDesc} VO对象
    * @return 添加后的${sourceTableDesc} VO对象
    */
    @Override
    public ${underlineToCamel(sourceTable?lower_case,true?string)}Vo add(${underlineToCamel(sourceTable?lower_case,true?string)}Vo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        ${underlineToCamel(sourceTable?lower_case,true?string)}Entity entity = ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.convertToTarget(vo);
        if(this.save(entity)){
            return ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("添加 ${sourceTableDesc} 单个对象失败!");
            //return null;
        }
    }

    /**
    * 添加${sourceTableDesc}集合列表到数据库
    * @param vos  ${sourceTableDesc} VO对象列表
    * @return 添加后的${sourceTableDesc} VO对象集合列表
    */
    @Override
    public List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> adds(List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<${underlineToCamel(sourceTable?lower_case,true?string)}Entity> entities = ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.convertToTarget(vos);
        if(this.saveBatch(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
           throw new RuntimeException("添加 ${sourceTableDesc} 集合列表失败!");
           //return null;
        }
    }

    /**
    * 更新${sourceTableDesc}到数据库
    * @param vo 要更新的${sourceTableDesc} VO对象
    * @return 更新后的${sourceTableDesc} VO对象
    */
    @Override
    public ${underlineToCamel(sourceTable?lower_case,true?string)}Vo modify(${underlineToCamel(sourceTable?lower_case,true?string)}Vo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        ${underlineToCamel(sourceTable?lower_case,true?string)}Entity entity = ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.convertToTarget(vo);
        if(this.updateById(entity)){
            return ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("更新${sourceTableDesc}单个对象失败!");
            //return null;
        }
    }

    /**
    * 更新${sourceTableDesc}集合列表到数据库
    * @param vos 要更新的${sourceTableDesc} VO对象集合
    * @return 更新后的${sourceTableDesc} VO对象集合
    */
    @Override
    public List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> modifys(List<${underlineToCamel(sourceTable?lower_case,true?string)}Vo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<${underlineToCamel(sourceTable?lower_case,true?string)}Entity> entities = ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.convertToTarget(vos);
        if(this.updateBatchById(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return ${underlineToCamel(sourceTable?lower_case,false?string)}VoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
            throw new RuntimeException("更新${sourceTableDesc}集合列表失败!");
            //return null;
        }
    }	

}